<?php
namespace Admin\Controller;
use \Admin\Controller\AdminController;

class WebuserController extends AdminController
{
	//用户列表页
	public function index()
	{
		$where = '';
		$model =  M('bar_use');

		if(!empty($_GET)){
			$name = urldecode(I('get.name'));
			// var_dump($name);
			$where['username'] = array('like','%'.$name.'%');
			$map['username'] = $name; 
			$this->assign('where',$name);
		}

		$count = $model->where($where)->count();
		$Page = new \Think\Page($count,5);
		$Page->lastSuffix = false;
		//定制分页样式
		$Page->setConfig('header','共有 %TOTAL_ROW% 个用户');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');
		//修改主题
		$Page->setConfig('theme',' <ul class="pagination"><li>%FIRST%</li> <li>%UP_PAGE%</li> <li>%LINK_PAGE%</li> <li>%DOWN_PAGE%</li> <li>%END%</li></ul>');

		foreach($map as $key=>$val) {
			$Page->parameter[$key] = urlencode($val);
		}

		$show = $Page->show();
		$data = $model->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->where($where)->select();
		$this->assign('title','用户管理系统');
		$this->assign('list',$data);
		$this->assign('page',$show);
		$this->display('index');
	}

	public function add()
	{
		$this->assign('title','用户添加');
		$this->display();
	}

	public function insert()
	{
		$model = M('bar_use');
		if($model->create()) {
			$result = $model->add();
			if($result) {
				$this->success('数据添加成功！',U('index'));
			}else{
				$this->error('数据添加错误！');
			}
		}else{
			$this->error('数据有误！');
		}

	}

	public function edit()
	{
		 $id = I('get.id');
        //var_dump($id);
		$data = M('bar_use')->find($id);
       // var_dump($data);
		if(is_null($data)){
			$this->redirect('index');
			return;
		}
		$this->assign('title','用户修改');
		$this->assign('list',$data);
		$this->display();
	}

	public function save()
	{
		$model = M('bar_use');
		if($model->create()) {
			$result = $model->save();
			if($result) {
				$this->success('数据修改成功！',U('index'));
			}else{
				$this->error('数据修改错误！',U('edit',array('id'=>I('post.id'))));
			}
		}else{
			$this->error('数据有误！');
		}

	}
	public function del()
	{
		if(!IS_AJAX){
			$this->error('请用正确的方式提交',U('bar_user/index'));
			return;
		}
		//强制转换为整型，默认值为0
		$id = I('get.id/d',0);

		if(M('bar_use')->delete($id)){
			echo 1;
		}else{
			echo 0;
		}
	}
	public function more()
	{
		 $id = I('get.id');
        // var_dump($id);
		$data = M('bar_use')->find($id);
       // var_dump($data);
		if(is_null($data)){
			$this->redirect('index');
			return;
		}
		$this->assign('title','用户修改');
		$this->assign('list',$data);
		$this->display();
	}
}